package bb.lanxing.network;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

public class ApiRequestBuilder {
    public static String API_DOMAIN = null;
    public static String API_DOMAIN_BASE = null;
    public static String API_DOMAIN_WWW = "https://woc.bluebank.com.cn/api/";
    private static final MediaType MEDIA_TYPE_GZIP = MediaType.parse(HttpProxy.MEDIA_TYPE_GZIP);

    public static void init(String str) {
        if (str == null) {
            str = "http://api.bluebank.com.cn/";
        }
        API_DOMAIN_BASE = str;
        API_DOMAIN = API_DOMAIN_BASE + "api/";
    }

    public static void init() {
        init(null);
    }

    private static Request.Builder getApiRequestBuilder(String str) {
        if (MyHttpClient.client == null) {
            throw new IllegalStateException("BiCi http client is null");
        }
        Request.Builder builder = new Request.Builder();
        return builder.url(API_DOMAIN + str).header("User-Agent", MyHttpClient.userAgent).addHeader("Accept", "application/json").addHeader("Content-type", "application/json");
    }

    private static Request.Builder getWWWApiRequestBuilder(String str) {
        if (MyHttpClient.client == null) {
            throw new IllegalStateException("BiCi http client is null");
        }
        Request.Builder builder = new Request.Builder();
        return builder.url(API_DOMAIN_WWW + str).header("User-Agent", MyHttpClient.userAgent).addHeader("Accept", "application/json").addHeader("Content-type", "application/json");
    }

    private static Call newCall(Request request) {
        return MyHttpClient.client.newCall(request);
    }

    public static Call get(String str, Map<String, Object> map) {
        return newCall(getApiRequestBuilder(MyHttpClient.buildUrlByParams(str, map)).build());
    }

    public static Call post(String str, Map<String, Object> map) {
        boolean z;
        Request.Builder apiRequestBuilder = getApiRequestBuilder(str);
        Iterator<Object> it = map.values().iterator();
        while (true) {
            if (it.hasNext()) {
                if (it.next() instanceof File) {
                    z = true;
                    break;
                }
            } else {
                z = false;
                break;
            }
        }
        if (z) {
            MultipartBody.Builder builder = new MultipartBody.Builder("bicibike");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Object value = entry.getValue();
                if (value instanceof File) {
                    File file = (File) value;
                    builder.addFormDataPart(entry.getKey(), file.getName(), RequestBody.create(MEDIA_TYPE_GZIP, file));
                } else {
                    builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
                }
            }
            apiRequestBuilder.post(builder.build());
        } else {
            FormBody.Builder builder2 = new FormBody.Builder();
            for (Map.Entry<String, Object> entry2 : map.entrySet()) {
                builder2.add(entry2.getKey(), entry2.getValue().toString());
            }
            apiRequestBuilder.post(builder2.build());
        }
        return newCall(apiRequestBuilder.build());
    }

    public static Call wwwget(String str, Map<String, Object> map) {
        return newCall(getWWWApiRequestBuilder(MyHttpClient.buildUrlByParams(str, map)).build());
    }

    public static Call wwwpost(String str, Map<String, Object> map) {
        boolean z;
        Request.Builder wWWApiRequestBuilder = getWWWApiRequestBuilder(str);
        Iterator<Object> it = map.values().iterator();
        while (true) {
            if (it.hasNext()) {
                if (it.next() instanceof File) {
                    z = true;
                    break;
                }
            } else {
                z = false;
                break;
            }
        }
        if (z) {
            MultipartBody.Builder builder = new MultipartBody.Builder("bicibike");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Object value = entry.getValue();
                if (value instanceof File) {
                    File file = (File) value;
                    builder.addFormDataPart(entry.getKey(), file.getName(), RequestBody.create(MEDIA_TYPE_GZIP, file));
                } else {
                    builder.addFormDataPart(entry.getKey(), entry.getValue().toString());
                }
            }
            wWWApiRequestBuilder.post(builder.build());
        } else {
            FormBody.Builder builder2 = new FormBody.Builder();
            for (Map.Entry<String, Object> entry2 : map.entrySet()) {
                builder2.add(entry2.getKey(), entry2.getValue().toString());
            }
            wWWApiRequestBuilder.post(builder2.build());
        }
        return newCall(wWWApiRequestBuilder.build());
    }
}
